#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
批量自动化脚本 - 循环执行多组经纬度
"""

import os
import time
import json
import glob
from datetime import datetime
from browser_automation import BrowserAutomation

class BatchAutomation:
    def __init__(self):
        """初始化批量自动化类"""
        self.automation_script = "browser_automation.py"
        self.screenshots_dir = "screenshots"
        self.results = []
        
        # GIF生成质量配置
        self.gif_quality_level = 2    # 1=最小，2=平衡，3=最大
        self.gif_resize_ratio = 0.7  # 缩放比例（0.5 = 50%）
        self.gif_color_count = 64     # 颜色数量
        
    def setup_coordinates(self):
        """设置多组经纬度数据"""
        coordinates = [
           {
               "lng": 116.689545,
               "lat": 26.206964,
               "level": 17,
               "name": "福建建宁汇能环保科技有限公司"
           },
           {
               "lng": 107.079212,
               "lat": 39.375056,
               "level": 17,
               "name": "鄂托克前旗敖镇生活垃圾填埋场"
           },
           {
               "lng": 113.6532,
               "lat": 34.408142,
               "level": 17,
               "name": "曹庄垃圾填埋场（已封场）"
           },
           {
               "lng": 113.216625,
               "lat": 24.271504,
               "level": 17,
               "name": "英德市老虎岩生活垃圾处理有限公司"
           },
           {
               "lng": 116.615713,
               "lat": 39.943047,
               "level": 17,
               "name": "北京市朝阳区高安屯卫生填埋场"
           },
           {
               "lng": 116.522274,
               "lat": 39.627758,
               "level": 17,
               "name": "北京环境卫生工程集团有限公司安定园区运营管理分公司安定垃圾卫生填埋场"
           },
           {
               "lng": 116.1189,
               "lat": 39.613646,
               "level": 17,
               "name": "北京市东南召垃圾填埋场"
           },
           {
               "lng": 108.568731,
               "lat": 32.52453,
               "level": 17,
               "name": "紫阳县城区生活垃圾处理工程"
           },
           {
               "lng": 115.535997,
               "lat": 33.902507,
               "level": 17,
               "name": "鹿邑县城市生活垃圾处理场"
           },
           {
               "lng": 116.22283,
               "lat": 40.058514,
               "level": 17,
               "name": "北京市海盛金池垃圾填埋厂（半壁店）"
           },
           {
               "lng": 116.031144,
               "lat": 39.678929,
               "level": 17,
               "name": "房山区田各庄生活垃圾填埋场"
           },
           {
               "lng": 115.120905,
               "lat": 33.41969,
               "level": 17,
               "name": "沈丘县蓝天城市生活垃圾处理场"
           },
           {
               "lng": 114.808752,
               "lat": 34.045847,
               "level": 17,
               "name": "太康县城市生活垃圾处理场"
           },
           {
               "lng": 106.058225,
               "lat": 30.703103,
               "level": 17,
               "name": "蓬安垃圾处理有限公司"
           },
           {
               "lng": 116.222615,
               "lat": 40.058304,
               "level": 17,
               "name": "北京市海淀区六里屯垃圾填埋场"
           },
           {
               "lng": 115.536406,
               "lat": 33.902334,
               "level": 17,
               "name": "项城市城市生活垃圾处理厂"
           },
           {
               "lng": 114.639155,
               "lat": 33.009505,
               "level": 17,
               "name": "平舆县垃圾处理有限公司"
           },
           {
               "lng": 104.305487,
               "lat": 29.662864,
               "level": 17,
               "name": "威远县垃圾处理有限责任公司"
           },
           {
               "lng": 107.400137,
               "lat": 33.117669,
               "level": 17,
               "name": "城固县城市垃圾处理厂"
           },
           {
               "lng": 116.61838,
               "lat": 39.729832,
               "level": 17,
               "name": "北京通州京环环境服务有限公司-生活垃圾填埋场（西田阳垃圾卫生填埋场）"
           },
           {
               "lng": 116.201542,
               "lat": 40.541134,
               "level": 17,
               "name": "北京市延庆区环境卫生保洁中心（永宁镇生活垃圾卫生填埋场）"
           },
           {
               "lng": 106.646448,
               "lat": 33.183231,
               "level": 17,
               "name": "勉县城市垃圾处理厂"
           },
           {
               "lng": 101.747915,
               "lat": 26.571653,
               "level": 17,
               "name": "攀枝花市二滩库区垃圾处理中心"
           },
           {
               "lng": 115.011428,
               "lat": 24.710403,
               "level": 17,
               "name": "定南县生活垃圾卫生填埋场"
           },
           {
               "lng": 107.400166,
               "lat": 33.117752,
               "level": 17,
               "name": "西乡县城市垃圾处理场管理站"
           },
           {
               "lng": 109.423891,
               "lat": 30.263587,
               "level": 17,
               "name": "恩施市城市生活垃圾填埋场"
           },
           {
               "lng": 107.40012,
               "lat": 33.117778,
               "level": 17,
               "name": "洋县城市垃圾处理厂"
           },
           {
               "lng": 124.566051,
               "lat": 49.711397,
               "level": 17,
               "name": "鄂伦春自治旗大杨树镇浩洁垃圾污水处理厂垃圾处理分厂"
           },
           {
               "lng": 116.583815,
               "lat": 38.13812,
               "level": 17,
               "name": "泊头市洁净垃圾填埋场"
           },
           {
               "lng": 109.975428,
               "lat": 33.848751,
               "level": 17,
               "name": "商洛市商州区垃圾处理场"
           },
           {
               "lng": 117.306842,
               "lat": 38.28396,
               "level": 17,
               "name": "黄骅市生活垃圾卫生填埋场"
           },
           {
               "lng": 104.57808,
               "lat": 28.775996,
               "level": 17,
               "name": "高县城市生活垃圾处理厂"
           },
           {
               "lng": 114.347398,
               "lat": 23.110279,
               "level": 17,
               "name": "红花岭垃圾填埋厂一、二、三期"
           },
           {
               "lng": 104.354294,
               "lat": 28.797989,
               "level": 17,
               "name": "四川同益环境科技有限责任公司屏山分公司(屏山县城生活垃圾填埋场)"
           },
           {
               "lng": 116.780752,
               "lat": 38.013505,
               "level": 17,
               "name": "南皮县清洁垃圾处理厂"
           },
           {
               "lng": 104.577869,
               "lat": 28.776001,
               "level": 17,
               "name": "兴文县江泓城市生活垃圾处理有限责任公司（县城生活垃圾填埋场）"
           },
           {
               "lng": 110.156032,
               "lat": 34.574638,
               "level": 17,
               "name": "华阴市华岳垃圾处理厂"
           },
           {
               "lng": 114.639428,
               "lat": 26.403267,
               "level": 17,
               "name": "遂川遂卫保洁有限公司"
           },
           {
               "lng": 119.533125,
               "lat": 31.774861,
               "level": 17,
               "name": "常州市金坛区沈渎生活垃圾卫生填埋场"
           },
           {
               "lng": 111.550637,
               "lat": 22.815022,
               "level": 17,
               "name": "罗定市第一生活垃圾处理有限公司"
           },
           {
               "lng": 104.880981,
               "lat": 29.188275,
               "level": 17,
               "name": "自贡市城乡垃圾处理中心"
           },
           {
               "lng": 119.017193,
               "lat": 33.489663,
               "level": 17,
               "name": "淮安市王元生活垃圾填埋场"
           },
           {
               "lng": 110.299065,
               "lat": 21.1869,
               "level": 17,
               "name": "湛江市生活垃圾处理场"
           },
           {
               "lng": 110.093036,
               "lat": 21.250828,
               "level": 17,
               "name": "遂溪县泰裕环保工程有限公司（溪县生活垃圾无害化填埋场）"
           },
           {
               "lng": 119.178577,
               "lat": 34.705685,
               "level": 17,
               "name": "连云港市环境卫生管理处（连云港市刘湾生活垃圾填埋场）"
           },
           {
               "lng": 108.63578,
               "lat": 34.35373,
               "level": 17,
               "name": "彬州市生活垃圾填埋场"
           },
           {
               "lng": 90.178445,
               "lat": 44.292675,
               "level": 17,
               "name": "木垒县生活垃圾填埋厂"
           },
           {
               "lng": 108.635768,
               "lat": 34.353758,
               "level": 17,
               "name": "淳化县城镇生活垃圾处理场"
           },
           {
               "lng": 118.568376,
               "lat": 31.763576,
               "level": 17,
               "name": "江南灰渣填埋场"
           },
           {
               "lng": 117.195955,
               "lat": 37.761492,
               "level": 17,
               "name": "乐陵市综合行政执法局生活垃圾处理场"
           },
           {
               "lng": 108.635734,
               "lat": 34.353472,
               "level": 17,
               "name": "乾县生活垃圾卫生填埋场"
           },
           {
               "lng": 79.880353,
               "lat": 37.288922,
               "level": 17,
               "name": "和田市垃圾处理厂"
           },
           {
               "lng": 116.80643,
               "lat": 37.713283,
               "level": 17,
               "name": "宁津县洁宁垃圾处理有限公司"
           },
           {
               "lng": 108.63595,
               "lat": 34.353302,
               "level": 17,
               "name": "武功县生活垃圾处理厂"
           },
           {
               "lng": 118.444,
               "lat": 32.062654,
               "level": 17,
               "name": "南京环境集团有限公司（江北灰渣填埋场）"
           },
           {
               "lng": 106.589728,
               "lat": 26.723962,
               "level": 17,
               "name": "贵阳京环环保有限公司（比例坝生活垃圾填埋场及应急库）"
           },
           {
               "lng": 115.20592,
               "lat": 36.240528,
               "level": 17,
               "name": "大名县洁源垃圾处理开发有限公司"
           },
           {
               "lng": 114.838245,
               "lat": 36.560475,
               "level": 17,
               "name": "邯郸市肥乡区垃圾处理厂"
           },
           {
               "lng": 116.80639,
               "lat": 37.713298,
               "level": 17,
               "name": "夏津县城市生活垃圾处理场"
           },
           {
               "lng": 106.705717,
               "lat": 27.146486,
               "level": 17,
               "name": "贵州息烽城市生活垃圾处理厂"
           },
           {
               "lng": 108.556406,
               "lat": 36.638686,
               "level": 17,
               "name": "安塞区垃圾卫生填埋场"
           },
           {
               "lng": 114.998483,
               "lat": 36.508398,
               "level": 17,
               "name": "广平县生活垃圾填埋场"
           },
           {
               "lng": 109.552074,
               "lat": 36.553356,
               "level": 17,
               "name": "延安市垃圾处理场"
           },
           {
               "lng": 116.26917,
               "lat": 37.711814,
               "level": 17,
               "name": "景县康保垃圾处理场"
           },
           {
               "lng": 115.690888,
               "lat": 38.20832,
               "level": 17,
               "name": "饶阳县尚佳生活垃圾处理有限公司"
           },
           {
               "lng": 118.630448,
               "lat": 33.89258,
               "level": 17,
               "name": "沭阳县生活垃圾卫生填埋场"
           },
           {
               "lng": 108.040824,
               "lat": 21.579423,
               "level": 17,
               "name": "东兴市垃圾处理厂"
           },
           {
               "lng": 108.63568,
               "lat": 34.353506,
               "level": 17,
               "name": "曹新庄生活垃圾填埋场"
           },
           {
               "lng": 116.02474,
               "lat": 38.041344,
               "level": 17,
               "name": "武强县生活垃圾卫生填埋场"
           },
           {
               "lng": 118.172232,
               "lat": 33.541936,
               "level": 17,
               "name": "泗洪县生活卫生填埋场"
           },
           {
               "lng": 108.792253,
               "lat": 27.203765,
               "level": 17,
               "name": "贵州嘉诚建设投资有限公司（榕江县生活垃圾卫生填埋场）"
           },
           {
               "lng": 118.630958,
               "lat": 33.89368,
               "level": 17,
               "name": "泗阳县生活垃圾填埋场"
           },
           {
               "lng": 116.327307,
               "lat": 36.594687,
               "level": 17,
               "name": "茌平县新环生活垃圾无害化处理厂"
           },
           {
               "lng": 116.27916,
               "lat": 39.294945,
               "level": 17,
               "name": "固安生活垃圾填埋场（牛驼）"
           },
           {
               "lng": 116.964805,
               "lat": 39.958545,
               "level": 17,
               "name": "三河市生活垃圾卫生填埋场"
           },
           {
               "lng": 122.783885,
               "lat": 46.705553,
               "level": 17,
               "name": "扎赉特旗西山垃圾填埋场"
           },
           {
               "lng": 104.577563,
               "lat": 35.559895,
               "level": 17,
               "name": "定西市城市生活垃圾处理场"
           },
           {
               "lng": 116.973973,
               "lat": 34.286128,
               "level": 17,
               "name": "徐州市环境卫生管理中心（雁群生活垃圾填埋场）"
           },
           {
               "lng": 103.823264,
               "lat": 35.37373,
               "level": 17,
               "name": "临洮县环境卫生管理所（临洮县生活垃圾填埋场）"
           },
           {
               "lng": 117.055335,
               "lat": 39.726047,
               "level": 17,
               "name": "香河安洁垃圾填埋有限公司"
           },
           {
               "lng": 118.421713,
               "lat": 34.40511,
               "level": 17,
               "name": "新沂市北马陵垃圾填埋场"
           },
           {
               "lng": 117.979323,
               "lat": 35.214433,
               "level": 17,
               "name": "费县城市生活垃圾处理厂"
           },
           {
               "lng": 120.002562,
               "lat": 33.424228,
               "level": 17,
               "name": "盐城大丰生活垃圾卫生填埋场"
           },
           {
               "lng": 111.277123,
               "lat": 32.418414,
               "level": 17,
               "name": "十堰市西部垃圾填埋场"
           },
           {
               "lng": 109.833976,
               "lat": 32.419603,
               "level": 17,
               "name": "竹山县兴竹环境科技有限公司（秦古镇垃圾填埋场）"
           },
           {
               "lng": 118.035398,
               "lat": 35.722244,
               "level": 17,
               "name": "蒙阴县生活垃圾处理场"
           },
           {
               "lng": 108.379473,
               "lat": 24.673874,
               "level": 17,
               "name": "河池市德胜生活垃圾处理场"
           },
           {
               "lng": 87.860909,
               "lat": 44.042338,
               "level": 17,
               "name": "乌鲁木齐市京环环境能源有限公司-米东固废综合处理厂及配套设施项目"
           },
           {
               "lng": 115.14806,
               "lat": 38.21821,
               "level": 17,
               "name": "辛集市美洁垃圾处理有限公司"
           },
           {
               "lng": 114.315043,
               "lat": 38.36312,
               "level": 17,
               "name": "灵寿县松阳垃圾处理场"
           },
           {
               "lng": 119.86772,
               "lat": 32.229447,
               "level": 17,
               "name": "扬中市年丰垃圾渗滤液处理场"
           },
           {
               "lng": 106.862342,
               "lat": 27.540436,
               "level": 17,
               "name": "播州区垃圾填埋场"
           },
           {
               "lng": 98.262723,
               "lat": 39.945185,
               "level": 17,
               "name": "嘉峪关市市容环境卫生总站（嘉峪关市断山口生活垃圾处理场、嘉峪关市八昼夜滩生活垃圾处理场）"
           },
           {
               "lng": 81.154825,
               "lat": 43.978429,
               "level": 17,
               "name": "伊宁县垃圾填埋场"
           },
           {
               "lng": 118.071348,
               "lat": 39.389378,
               "level": 17,
               "name": "唐山市尖字沽生活垃圾填埋场"
           },
           {
               "lng": 121.895617,
               "lat": 39.641749,
               "level": 17,
               "name": "瓦房店垃圾处理厂"
           },
           {
               "lng": 114.210402,
               "lat": 30.355336,
               "level": 17,
               "name": "武汉诺洁环境工程有限公司(长山口生活垃圾卫生填埋场）"
           },
           {
               "lng": 124.085721,
               "lat": 40.025717,
               "level": 17,
               "name": "东港市赤榆城市垃圾填埋有限公司"
           },
           {
               "lng": 119.783706,
               "lat": 31.00231,
               "level": 17,
               "name": "长兴县生活垃圾应急填埋场"
           },
           {
               "lng": 117.765038,
               "lat": 39.862335,
               "level": 17,
               "name": "玉田县城市管理综合执法大队(生活垃圾填埋场)"
           },
           {
               "lng": 120.818433,
               "lat": 30.368931,
               "level": 17,
               "name": "海宁市黄湾卫生填埋场"
           },
           {
               "lng": 120.857205,
               "lat": 40.856323,
               "level": 17,
               "name": "八家子经济开发区垃圾填埋场"
           },
           {
               "lng": 114.718152,
               "lat": 37.50566,
               "level": 17,
               "name": "柏乡县恒洁垃圾处理有限公司"
           },
           {
               "lng": 112.707499,
               "lat": 32.083967,
               "level": 17,
               "name": "枣阳市西洼垃圾场"
           },
           {
               "lng": 115.177655,
               "lat": 37.060363,
               "level": 17,
               "name": "广宗县城管垃圾清运服务有限公司"
           },
           {
               "lng": 107.475008,
               "lat": 28.546636,
               "level": 17,
               "name": "贵州杉海沿环保科技有限责任公司正安分公司（正安县城镇生活垃圾卫生填埋场）"
           },
           {
               "lng": 115.465608,
               "lat": 36.888845,
               "level": 17,
               "name": "临西县临洁垃圾处理有限公司"
           },
           {
               "lng": 120.29912,
               "lat": 29.229724,
               "level": 17,
               "name": "东阳市环境卫生有限公司（东阳市第二生活垃圾卫生填埋场）"
           },
           {
               "lng": 120.515783,
               "lat": 37.354705,
               "level": 17,
               "name": "栖霞市城区生活垃圾处理场"
           },
           {
               "lng": 120.983184,
               "lat": 41.080643,
               "level": 17,
               "name": "黑山县城区生活垃圾卫生填埋场"
           },
           {
               "lng": 113.705745,
               "lat": 31.131718,
               "level": 17,
               "name": "云梦县城乡生活垃圾管理服务中心"
           },
           {
               "lng": 115.371593,
               "lat": 37.302943,
               "level": 17,
               "name": "南宫市生活垃圾处理厂"
           },
           {
               "lng": 103.979483,
               "lat": 36.024958,
               "level": 17,
               "name": "榆中县垃圾处理场"
           },
           {
               "lng": 120.438867,
               "lat": 29.078627,
               "level": 17,
               "name": "磐安县台口生活垃圾填埋场"
           },
           {
               "lng": 114.492245,
               "lat": 35.602466,
               "level": 17,
               "name": "滑县美洁垃圾处理厂"
           },
           {
               "lng": 119.871138,
               "lat": 29.604745,
               "level": 17,
               "name": "浦江县小黄坛填埋场"
           },
           {
               "lng": 114.86654,
               "lat": 35.887718,
               "level": 17,
               "name": "内黄县城市管理局（内黄县高庙垃圾综合处理有限公司）"
           },
           {
               "lng": 115.02329,
               "lat": 37.030955,
               "level": 17,
               "name": "平乡县益民垃圾处理中心"
           },
           {
               "lng": 101.677635,
               "lat": 25.0247,
               "level": 17,
               "name": "楚雄市洁净垃圾填埋处理厂"
           },
           {
               "lng": 119.942324,
               "lat": 28.903257,
               "level": 17,
               "name": "永康市环境卫生管理处（永康市花川垃圾填埋场）"
           },
           {
               "lng": 110.068403,
               "lat": 19.931157,
               "level": 17,
               "name": "海口市垃圾处理场（海口市颜春岭垃圾填埋场）"
           },
           {
               "lng": 114.127931,
               "lat": 35.622179,
               "level": 17,
               "name": "鹤壁市生活垃圾填埋场"
           },
           {
               "lng": 106.046582,
               "lat": 35.236666,
               "level": 17,
               "name": "泾川县城区生活垃圾填埋场"
           },
           {
               "lng": 111.366431,
               "lat": 30.382272,
               "level": 17,
               "name": "宜都市城市管理执法局（吴家湾垃圾填理场）"
           },
           {
               "lng": 112.984561,
               "lat": 35.031268,
               "level": 17,
               "name": "沁阳市城市生活垃圾卫生填埋场"
           },
           {
               "lng": 114.900227,
               "lat": 34.801108,
               "level": 17,
               "name": "兰考县城市生活垃圾处理场"
           },
           {
               "lng": 114.805355,
               "lat": 34.539682,
               "level": 17,
               "name": "杞县永洁垃圾处理厂"
           },
           {
               "lng": 106.03993,
               "lat": 29.933798,
               "level": 17,
               "name": "重庆财信环境资源股份有限公司合川城市生活垃圾处理厂"
           },
           {
               "lng": 106.633898,
               "lat": 29.639344,
               "level": 17,
               "name": "重庆市固体废弃物处理有限公司（黑石子垃圾处理场）"
           },
           {
               "lng": 121.425517,
               "lat": 29.6002,
               "level": 17,
               "name": "奉化张家岙垃圾填埋场"
           },
           {
               "lng": 111.620758,
               "lat": 33.795049,
               "level": 17,
               "name": "栾川县振兴市政工程有限公司（栾川县叫河镇城市生活垃圾处理工程）"
           },
           {
               "lng": 123.906192,
               "lat": 42.325952,
               "level": 17,
               "name": "铁岭柴河垃圾处理厂"
           },
           {
               "lng": 121.383695,
               "lat": 29.752968,
               "level": 17,
               "name": "海曙野猫岙垃圾填埋场"
           },
           {
               "lng": 122.742622,
               "lat": 42.01296,
               "level": 17,
               "name": "安民镇垃圾填埋场"
           },
           {
               "lng": 121.91869,
               "lat": 29.35986,
               "level": 17,
               "name": "象山县水桶岙垃圾填埋场"
           },
           {
               "lng": 112.622598,
               "lat": 27.026626,
               "level": 17,
               "name": "衡阳市环境卫生事务中心（衡阳市第二生活垃圾填埋场）"
           },
           {
               "lng": 100.494194,
               "lat": 39.048332,
               "level": 17,
               "name": "张掖市甘州区城市垃圾处理厂"
           },
           {
               "lng": 105.575618,
               "lat": 29.391007,
               "level": 17,
               "name": "重庆财信环境资源股份有限公司荣昌城市生活垃圾处理厂"
           },
           {
               "lng": 121.057518,
               "lat": 30.285698,
               "level": 17,
               "name": "余姚久能原应急垃圾填埋场"
           },
           {
               "lng": 99.719651,
               "lat": 27.75505,
               "level": 17,
               "name": "香格里拉市垃圾填埋场"
           },
           {
               "lng": 122.430011,
               "lat": 40.435366,
               "level": 17,
               "name": "盖州市生活垃圾处理场"
           },
           {
               "lng": 103.280423,
               "lat": 23.736693,
               "level": 17,
               "name": "开远市城市生活垃圾处理有限责任公司"
           },
           {
               "lng": 107.668216,
               "lat": 29.893673,
               "level": 17,
               "name": "重庆丰都环卫集团有限公司（丰都县城市生活垃圾卫生填埋场）"
           },
           {
               "lng": 126.456164,
               "lat": 43.898777,
               "level": 17,
               "name": "蛟河市洁美垃圾处理厂"
           },
           {
               "lng": 120.501379,
               "lat": 28.021772,
               "level": 17,
               "name": "温州市西向生态填埋场"
           },
           {
               "lng": 126.021981,
               "lat": 45.01319,
               "level": 17,
               "name": "扶余市城市垃圾处理有限责任公司"
           },
           {
               "lng": 108.993124,
               "lat": 28.789446,
               "level": 17,
               "name": "重庆市财信环保投资股份有限公司酉阳县城市垃圾处理厂"
           },
           {
               "lng": 120.134642,
               "lat": 27.778806,
               "level": 17,
               "name": "文成县垃圾填埋场"
           },
           {
               "lng": 111.466425,
               "lat": 33.24526,
               "level": 17,
               "name": "南阳首创环境科技有限公司五里桥镇垃圾填埋场"
           },
           {
               "lng": 112.975892,
               "lat": 27.76235,
               "level": 17,
               "name": "湘潭市环境卫生管理处双马镇生活垃圾填埋场"
           },
           {
               "lng": 116.117702,
               "lat": 30.18623,
               "level": 17,
               "name": "安徽省宿松县生活垃圾填埋场"
           },
           {
               "lng": 117.468486,
               "lat": 32.896853,
               "level": 17,
               "name": "蚌埠市环境卫生管理处（蚌埠市生活垃圾卫生填埋场）"
           },
           {
               "lng": 116.20175,
               "lat": 33.087463,
               "level": 17,
               "name": "利辛县垃圾填埋场"
           },
           {
               "lng": 116.201757,
               "lat": 33.087538,
               "level": 17,
               "name": "亳州市生活垃圾填埋场"
           },
           {
               "lng": 125.36248,
               "lat": 45.755525,
               "level": 17,
               "name": "肇州县和平垃圾处理有限公司"
           },
           {
               "lng": 107.117504,
               "lat": 31.482619,
               "level": 17,
               "name": "平昌县鑫鑫环境治理有限公司（平昌县垃圾处理厂）"
           },
           {
               "lng": 112.214722,
               "lat": 37.653758,
               "level": 17,
               "name": "清徐县生活垃圾填埋场"
           },
           {
               "lng": 112.571938,
               "lat": 29.591487,
               "level": 17,
               "name": "华容首创垃圾综合处理有限责任公司"
           },
           {
               "lng": 103.686474,
               "lat": 31.073117,
               "level": 17,
               "name": "都江堰市生活垃圾处置中心"
           },
           {
               "lng": 117.086831,
               "lat": 31.695213,
               "level": 17,
               "name": "肥西县生活垃圾填埋场"
           },
           {
               "lng": 112.850488,
               "lat": 33.755117,
               "level": 17,
               "name": "鲁山县生活垃圾处理场"
           },
           {
               "lng": 113.367648,
               "lat": 33.798427,
               "level": 17,
               "name": "平顶山市垃圾处理场"
           },
           {
               "lng": 115.553175,
               "lat": 35.868428,
               "level": 17,
               "name": "范县垃圾处理有限责任公司"
           },
           {
               "lng": 113.215019,
               "lat": 27.842581,
               "level": 17,
               "name": "株洲市丰源环境治理有限责任公司"
           },
           {
               "lng": 113.729066,
               "lat": 22.868831,
               "level": 17,
               "name": "东莞市虎门镇五马生活垃圾填埋（北京轩昂环保科技股份有限公司）"
           },
           {
               "lng": 114.10063,
               "lat": 22.889071,
               "level": 17,
               "name": "东莞市樟木头镇生活垃圾填埋场（樟木头公用事业服务中心）"
           },
           {
               "lng": 110.894857,
               "lat": 34.616628,
               "level": 17,
               "name": "灵宝市函谷垃圾处理厂"
           },
           {
               "lng": 118.634536,
               "lat": 31.659969,
               "level": 17,
               "name": "马鞍山市环境卫生管理处向山垃圾处理场"
           },
           {
               "lng": 113.483264,
               "lat": 23.263479,
               "level": 17,
               "name": "广州环投环境服务有限公司(兴丰应急填埋场+兴丰生活垃圾卫生填埋场+污染土壤集中治理与资源化利用处置中"
           },
           {
               "lng": 118.384208,
               "lat": 31.253174,
               "level": 17,
               "name": "芜湖市垃圾处理厂"
           },
           {
               "lng": 112.852274,
               "lat": 36.061708,
               "level": 17,
               "name": "长子县生活垃圾卫生填埋场"
           },
           {
               "lng": 117.907352,
               "lat": 33.567919,
               "level": 17,
               "name": "泗县泗州生活垃圾处理厂"
           },
           {
               "lng": 116.931988,
               "lat": 34.142118,
               "level": 17,
               "name": "萧县生活垃圾处理厂"
           },
           {
               "lng": 115.15555,
               "lat": 34.614945,
               "level": 17,
               "name": "民权县生活垃圾处理场"
           },
           {
               "lng": 115.294573,
               "lat": 34.401547,
               "level": 17,
               "name": "宁陵县城市生活垃圾处理场"
           },
           {
               "lng": 113.550867,
               "lat": 23.156701,
               "level": 17,
               "name": "新塘镇陈家林垃圾场（侨银城市管理股份有限公司）"
           },
            {
                "lng": 118.545352,
                "lat": 30.045268,
                "level": 17,
                "name": "绩溪县南郊垃圾处理有限公司"
            },
            {
                "lng": 114.347311,
                "lat": 23.110188,
                "level": 17,
                "name": "钰华朗生态技术服务（惠州）有限公司（惠州市垃圾处理场）"
            },
            {
                "lng": 115.841113,
                "lat": 34.364593,
                "level": 17,
                "name": "虞城县曹路桥垃圾处理厂"
            },
            {
                "lng": 114.278842,
                "lat": 35.054462,
                "level": 17,
                "name": "封丘县城市生活垃圾无害化处理厂"
            },
            {
                "lng": 90.958615,
                "lat": 29.569858,
                "level": 17,
                "name": "西藏博瑞环卫服务有限公司（曲水县垃圾填埋场）"
            },
            {
                "lng": 113.874144,
                "lat": 35.404211,
                "level": 17,
                "name": "新乡市环美垃圾处理有限公司"
            },
            {
                "lng": 103.977194,
                "lat": 29.211579,
                "level": 17,
                "name": "犍为县绿环垃圾处理有限公司"
            },
            {
                "lng": 113.855412,
                "lat": 35.475974,
                "level": 17,
                "name": "辉县市洁美垃圾处理有限公司"
            },
            {
                "lng": 113.615893,
                "lat": 35.292703,
                "level": 17,
                "name": "获嘉县城市生活垃圾处理场"
            },
            {
                "lng": 106.549508,
                "lat": 37.373886,
                "level": 17,
                "name": "吴忠市太阳山城市生活垃圾填埋场"
            },
            {
                "lng": 117.007961,
                "lat": 25.144001,
                "level": 17,
                "name": "龙岩市环境卫生中心（黄竹坑垃圾填埋场）"
            },
            {
                "lng": 116.477063,
                "lat": 23.659957,
                "level": 17,
                "name": "深圳市前海东江环保科技服务有限公司揭阳分公司（揭阳市东径外草地垃圾填埋场）"
            },
            {
                "lng": 113.736051,
                "lat": 35.224494,
                "level": 17,
                "name": "新乡县生活垃圾填埋场"
            },
            {
                "lng": 115.859638,
                "lat": 23.394998,
                "level": 17,
                "name": "北京沁润环保科技有限公司（揭西县老虎坷垃圾填埋场）"
            },
            {
                "lng": 118.615167,
                "lat": 27.724608,
                "level": 17,
                "name": "浦城县水北生活垃圾卫生填埋场"
            },
            {
                "lng": 115.104478,
                "lat": 32.143113,
                "level": 17,
                "name": "潢川县城市生活垃圾处理场"
            },
            {
                "lng": 113.999836,
                "lat": 32.462259,
                "level": 17,
                "name": "平桥区明港镇生活垃圾处理场"
            },
            {
                "lng": 116.178931,
                "lat": 24.302611,
                "level": 17,
                "name": "梅州市城市垃圾处理中心（梅州市奇龙坑垃圾填埋场）"
            },
            {
                "lng": 114.934449,
                "lat": 31.627817,
                "level": 17,
                "name": "新县垃圾处理中心"
            },
            {
                "lng": 113.911755,
                "lat": 34.166913,
                "level": 17,
                "name": "许昌市建安区城市生活垃圾处理场"
            },
            {
                "lng": 131.088434,
                "lat": 43.989857,
                "level": 17,
                "name": "宁安市庙岭垃圾处理厂"
            },
            {
                "lng": 118.300139,
                "lat": 25.502162,
                "level": 17,
                "name": "德化县环卫处（高内坑生活垃圾卫生填埋场）"
            },
            {
                "lng": 113.459108,
                "lat": 34.263367,
                "level": 17,
                "name": "禹州市城市发展中心(禹州市城市生活垃圾处理场)"
            },
            {
                "lng": 104.130766,
                "lat": 30.097062,
                "level": 17,
                "name": "仁寿县城市生活垃圾处理厂（仁寿县住房和城乡建设局）"
            },
            {
                "lng": 104.415645,
                "lat": 31.498613,
                "level": 17,
                "name": "塔水镇柑子村生活垃圾处理场"
            },
            {
                "lng": 104.424908,
                "lat": 31.599926,
                "level": 17,
                "name": "北川发展垃圾处理有限责任公司（北川新县城垃圾填埋场）"
            },
            {
                "lng": 104.716135,
                "lat": 31.303342,
                "level": 17,
                "name": "绵阳城区新建生活垃圾卫生填埋场（玉皇）"
            },
            {
                "lng": 116.999245,
                "lat": 39.2377,
                "level": 17,
                "name": "天津市双口生活垃圾卫生填埋场"
            },
            {
                "lng": 117.2739,
                "lat": 38.950776,
                "level": 17,
                "name": "天津市大韩庄垃圾填埋场"
            },
            {
                "lng": 117.397217,
                "lat": 39.645412,
                "level": 17,
                "name": "天津泉泰生活垃圾填埋场"
            },
            {
                "lng": 117.086973,
                "lat": 39.428033,
                "level": 17,
                "name": "武清雍泰生活垃圾填埋场"
            },
            {
                "lng": 117.724773,
                "lat": 39.174335,
                "level": 17,
                "name": "汉沽生活垃圾填埋场"
            },
            {
                "lng": 117.49975,
                "lat": 38.827043,
                "level": 17,
                "name": "大港生活垃圾填埋场"
            }

            

            # {
            #     "lng": 116.999245,
            #     "lat": 39.2377,
            #     "level": 17,
            #     "name": "双口"
            # },
            # {
            #     "lng": 117.2739,
            #     "lat": 38.950776,
            #     "level": 17,
            #     "name": "大韩庄"
            # },
            # {
            #     "lng": 117.397217,
            #     "lat": 39.645412,
            #     "level": 17,
            #     "name": "泉泰"
            # },
            # {
            #     "lng": 117.086973,
            #     "lat": 39.428033,
            #     "level": 17,
            #     "name": "雍泰"
            # },
            # {
            #     "lng": 117.724773,
            #     "lat": 39.174335,
            #     "level": 17,
            #     "name": "汉沽"
            # },
            # {
            #     "lng": 117.49975,
            #     "lat": 38.827043,
            #     "level": 17,
            #     "name": "大港"
            # }
        ]
        return coordinates
    
    def run_automation(self, lng, lat, level, name):
        """运行单次自动化（创建新的浏览器实例）"""
        try:
            print(f"\n🚀 开始处理坐标: {name} ({lng}, {lat})")
            
            # 创建自动化实例
            automation = BrowserAutomation()
            
            # 连接浏览器
            print("正在连接浏览器...")
            if not automation.connect_to_existing_browser():
                print("连接失败，启动新浏览器...")
                if not automation.setup_driver():
                    return False
            
            # 获取页面信息
            automation.get_page_info()
            
            # 等待页面加载
            print("等待页面完全加载...")
            time.sleep(1)  # 减少等待时间
            
            # 截取初始截图（直接使用命名规则）
            print("\n📸 截取初始页面截图")
            automation.take_screenshot(f"{lng}-{lat}-{name}-01_initial_page.jpg")
            
            # 执行自动化操作
            print("\n2. 开始自动化操作")
            
            # 点击定位按钮
            print("\n🔍 步骤1: 点击定位按钮")
            if automation.find_and_click_positioning_button():
                print("✅ 定位按钮点击成功")
                # 截图记录点击后的状态
                automation.take_screenshot(f"{lng}-{lat}-{name}-02_after_positioning_click.jpg")
            else:
                print("❌ 定位按钮点击失败")
                return False
            
            # 等待一下
            time.sleep(1)  # 减少等待时间
            
            # 处理弹出的定位窗口
            print("\n📍 步骤2: 处理定位弹窗")
            if automation.handle_positioning_popup_with_coords(lng, lat, level):
                print("✅ 定位弹窗处理成功")
                # 截图记录弹窗处理后的状态
                automation.take_screenshot(f"{lng}-{lat}-{name}-03_after_popup_input.jpg")
            else:
                print("❌ 定位弹窗处理失败")
                return False
            
            # 等待一下
            time.sleep(1)  # 减少等待时间
            
            # 处理时间轴（使用自定义命名）
            print("\n⏰ 步骤3: 处理时间轴")
            if automation.handle_timeline_with_naming(lng, lat, name):
                print("✅ 时间轴处理成功")
                # 截图记录时间轴处理后的状态
                automation.take_screenshot(f"{lng}-{lat}-{name}-04_after_timeline.jpg")
            else:
                print("❌ 时间轴处理失败")
            
            # 等待一下
            time.sleep(1)  # 减少等待时间
            
            # 最终截图
            print("\n📸 截取最终结果截图")
            automation.take_screenshot(f"{lng}-{lat}-{name}-05_final_result.jpg")
            
            print("\n✅ 自动化操作完成！")
            
            # 关闭浏览器
            automation.close_browser()
            
            return True
                
        except Exception as e:
            print(f"❌ 运行自动化失败: {e}")
            return False
    
    def run_automation_with_instance(self, automation, lng, lat, level, name):
        """使用现有的自动化实例运行单次自动化"""
        try:
            print(f"\n🚀 开始处理坐标: {name} ({lng}, {lat})")
            
            # 获取页面信息
            automation.get_page_info()
            
            # 等待页面加载
            print("等待页面完全加载...")
            time.sleep(1)  # 减少等待时间
            
            # 截取初始截图（直接使用命名规则）
            print("\n📸 截取初始页面截图")
            automation.take_screenshot(f"{lng}-{lat}-{name}-01_initial_page.jpg")
            
            # 执行自动化操作
            print("\n2. 开始自动化操作")
            
            # 点击定位按钮
            print("\n🔍 步骤1: 点击定位按钮")
            if automation.find_and_click_positioning_button():
                print("✅ 定位按钮点击成功")
                # 截图记录点击后的状态
                automation.take_screenshot(f"{lng}-{lat}-{name}-02_after_positioning_click.jpg")
            else:
                print("❌ 定位按钮点击失败")
                return False
            
            # 等待一下
            time.sleep(1)  # 减少等待时间
            
            # 处理弹出的定位窗口
            print("\n📍 步骤2: 处理定位弹窗")
            if automation.handle_positioning_popup_with_coords(lng, lat, level):
                print("✅ 定位弹窗处理成功")
                # 截图记录弹窗处理后的状态
                automation.take_screenshot(f"{lng}-{lat}-{name}-03_after_popup_input.jpg")
            else:
                print("❌ 定位弹窗处理失败")
                return False
            
            # 等待一下
            time.sleep(1)  # 减少等待时间
            
            # 处理时间轴（使用自定义命名）
            print("\n⏰ 步骤3: 处理时间轴")
            if automation.handle_timeline_with_naming(lng, lat, name):
                print("✅ 时间轴处理成功")
                # 截图记录时间轴处理后的状态
                automation.take_screenshot(f"{lng}-{lat}-{name}-04_after_timeline.jpg")
            else:
                print("❌ 时间轴处理失败")
            
            # 等待一下
            time.sleep(1)  # 减少等待时间
            
            # 最终截图
            print("\n📸 截取最终结果截图")
            automation.take_screenshot(f"{lng}-{lat}-{name}-05_final_result.jpg")
            
            print("\n✅ 自动化操作完成！")
            
            return True
                
        except Exception as e:
            print(f"❌ 运行自动化失败: {e}")
            return False
    
    def create_timeline_gif(self, lng, lat, name, use_optimized=True):
        """为当前坐标创建时间轴动图
        
        Args:
            lng: 经度
            lat: 纬度
            name: 地点名称
            use_optimized: 是否使用优化后的图片（默认False使用原始截图）
        """
        try:
            print(f"🎬 创建时间轴动图: {name}")
            
            # 导入动图生成模块
            from create_timeline_gif import create_timeline_gif
            
            if use_optimized:
                # 使用优化后的图片（在temp_optimized_frames文件夹中）
                print("📂 尝试使用优化后的图片...")
                pattern = f"temp_optimized_frames/{lng}-{lat}-{name}-*-optimized.jpg"
                
                # 检查优化图片是否存在
                optimized_files = glob.glob(f"screenshots/{pattern}")
                
                if optimized_files:
                    print(f"✓ 找到 {len(optimized_files)} 个优化图片")
                    gif_filename = f"{lng}-{lat}-{name}-timeline_animation_from_optimized.gif"
                else:
                    print("⚠️ 优化图片不存在，自动切换到原始截图")
                    print("提示: 首次运行时会从原始截图生成优化图片")
                    pattern = f"{lng}-{lat}-{name}-*.jpg"
                    gif_filename = f"{lng}-{lat}-{name}-timeline_animation_annotated.gif"
            else:
                # 使用原始截图
                print("📂 使用原始截图...")
                pattern = f"{lng}-{lat}-{name}-*.jpg"
                gif_filename = f"{lng}-{lat}-{name}-timeline_animation_annotated.gif"
            
            print(f"📋 Pattern: {pattern}")
            print(f"⚙️  质量配置: 等级{self.gif_quality_level}, 缩放{int(self.gif_resize_ratio*100)}%, {self.gif_color_count}色")
            success = create_timeline_gif(
                pattern=pattern,
                resize_ratio=self.gif_resize_ratio,
                color_count=self.gif_color_count,
                quality_level=self.gif_quality_level
            )
            
            if success:
                print(f"  ✓ 动图生成成功: {gif_filename}")
                return gif_filename
            else:
                print("  ✗ 动图生成失败")
                return None
            
        except Exception as e:
            print(f"✗ 创建动图失败: {e}")
            import traceback
            traceback.print_exc()
            return None
    
    
    def refresh_page(self, automation_instance):
        """刷新页面"""
        try:
            print("🔄 刷新页面...")
            
            # 使用传入的自动化实例刷新页面
            success = automation_instance.refresh_page()
            
            if success:
                print("✅ 页面刷新完成")
                return True
            else:
                print("✗ 页面刷新失败")
                return False
                
        except Exception as e:
            print(f"✗ 页面刷新失败: {e}")
            return False
    
    def run_batch_automation(self):
        """运行批量自动化"""
        try:
            print("=== 批量自动化开始 ===")
            
            # 获取坐标数据
            coordinates = self.setup_coordinates()
            print(f"📊 总共需要处理 {len(coordinates)} 组坐标")
            
            # 创建浏览器自动化实例（只创建一次）
            automation = BrowserAutomation()
            
            # 连接浏览器
            print("正在连接浏览器...")
            if not automation.connect_to_existing_browser():
                print("连接失败，启动新浏览器...")
                if not automation.setup_driver():
                    print("✗ 浏览器启动失败")
                    return False
            
            # 获取页面信息
            automation.get_page_info()
            
            # 循环处理每组坐标
            for i, coord in enumerate(coordinates):
                print(f"\n{'='*50}")
                print(f"📍 处理第 {i+1}/{len(coordinates)} 组坐标")
                print(f"坐标: {coord['lng']}, {coord['lat']}")
                print(f"级别: {coord['level']}")
                print(f"名称: {coord['name']}")
                print(f"{'='*50}")
                
                # 每次开始前强制刷新页面
                print(f"\n🔄 开始处理前强制刷新页面...")
                self.refresh_page(automation)
                
                # 运行自动化（使用现有实例）
                success = self.run_automation_with_instance(
                    automation,
                    coord['lng'], 
                    coord['lat'], 
                    coord['level'], 
                    coord['name']
                )
                
                if success:
                    # 创建时间轴动图
                    gif_filename = self.create_timeline_gif(
                        coord['lng'], 
                        coord['lat'], 
                        coord['name']
                    )
                    
                    # 记录结果
                    self.results.append({
                        "index": i + 1,
                        "name": coord['name'],
                        "lng": coord['lng'],
                        "lat": coord['lat'],
                        "level": coord['level'],
                        "gif_file": gif_filename,
                        "status": "成功"
                    })
                    
                    print(f"✅ 第 {i+1} 组坐标处理完成")
                else:
                    self.results.append({
                        "index": i + 1,
                        "name": coord['name'],
                        "lng": coord['lng'],
                        "lat": coord['lat'],
                        "level": coord['level'],
                        "status": "自动化失败"
                    })
                
                # 等待一下
                time.sleep(2)
            
            # 关闭浏览器
            automation.close_browser()
            
            # 输出结果清单
            self.print_results()
            
            return True
            
        except Exception as e:
            print(f"✗ 批量自动化失败: {e}")
            return False
    
    def print_results(self):
        """输出结果清单"""
        print(f"\n{'='*60}")
        print("📋 批量自动化结果清单")
        print(f"{'='*60}")
        
        for result in self.results:
            print(f"\n📍 第 {result['index']} 组坐标:")
            print(f"  名称: {result['name']}")
            print(f"  经度: {result['lng']}")
            print(f"  纬度: {result['lat']}")
            print(f"  级别: {result['level']}")
            print(f"  状态: {result['status']}")
            
            if result['status'] == "成功":
                if result['gif_file']:
                    print(f"  动图: {result['gif_file']}")
        
        print(f"\n{'='*60}")
        print("✅ 批量自动化完成！")
        print(f"📊 总计处理: {len(self.results)} 组坐标")
        print(f"✅ 成功: {len([r for r in self.results if r['status'] == '成功'])} 组")
        print(f"✗ 失败: {len([r for r in self.results if r['status'] != '成功'])} 组")
        print(f"{'='*60}")

def main():
    """主函数"""
    batch_automation = BatchAutomation()
    
    try:
        batch_automation.run_batch_automation()
    except KeyboardInterrupt:
        print("\n用户中断操作")
    except Exception as e:
        print(f"\n✗ 程序执行出错: {e}")

if __name__ == "__main__":
    main()
